package cn.gov.pbc.exam.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.excel.EasyExcel;

import cn.gov.pbc.exam.bo.UserBo;
import cn.gov.pbc.exam.common.Const;
import cn.gov.pbc.exam.common.ServerResponse;
import cn.gov.pbc.exam.dto.UserDto;
import cn.gov.pbc.exam.excel.QuestionExcelDto;
import cn.gov.pbc.exam.excel.QuestionExcelListener;
import cn.gov.pbc.exam.service.IQuestionService;
import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @author 黄松祥 2018年2月8日 下午12:25:21
 */
@RestController
@RequestMapping("/question/")
public class QuestionController {

	@Autowired
	private IQuestionService iQuestionService;

	/**
	 * 随机获取一个问题
	 * 
	 * @return
	 */
	@RequestMapping(value = "getQuestion.do", method = RequestMethod.POST)
	public ServerResponse<?> getQuestion(HttpSession session, UserDto userDto) {
		UserBo userBo = (UserBo) session.getAttribute(Const.CURRENT_USER);
		if (userBo == null) {
			userBo = new UserBo();
		}
		ServerResponse<?> response = iQuestionService.getQuestion(userBo, userDto);
		if(response.isSuccess()) {
			session.setAttribute(Const.CURRENT_USER, userBo);
		} else {
			session.invalidate();
		}
		return response;
	}

	/**
	 * 核对答案
	 * 
	 * @return
	 */
	@RequestMapping(value = "checkAnswer.do", method = RequestMethod.POST)
	public ServerResponse<?> checkAnswer(HttpSession session, 
			@RequestParam(value = "selection", defaultValue = "")String selection) {
		UserBo userBo = (UserBo)session.getAttribute(Const.CURRENT_USER);
		if(userBo == null) {
			return ServerResponse.createByErrorMessage("答题程序错误");
		}
		return iQuestionService.checkAnswer(userBo, selection);
	}
	
	@RequestMapping(value = "readExcel.do", method = RequestMethod.GET)
	public ServerResponse<?> readExcel(){
		
		return iQuestionService.readExcel();
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
